package com.xiaobao.coalproperty.other.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.xiaobao.coalproperty.R;
import com.xiaobao.coalproperty.module.user.model.entity.GoodsCollectListEntity;

import java.util.ArrayList;
import java.util.List;

public class UserCollectAdapter extends RecyclerView.Adapter<UserCollectAdapter.ViewHolder> {


    private Context mContext;
    private List<GoodsCollectListEntity.DataBean.ListBean> mAdapterData;

    public UserCollectAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public List<GoodsCollectListEntity.DataBean.ListBean> getAdapterData() {
        return mAdapterData;
    }

    public void setAdapterData(List<GoodsCollectListEntity.DataBean.ListBean> mAdapterData) {
        this.mAdapterData = mAdapterData;
        this.notifyDataSetChanged();
    }

    public void addAdapterData(List<GoodsCollectListEntity.DataBean.ListBean> mAdapterData) {
        if (this.mAdapterData == null) {
            this.mAdapterData = new ArrayList<>();
        }
        this.mAdapterData = mAdapterData;
        this.notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_adapter_user_collect, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        holder.tvName.setText(mAdapterData.get(position).getGoodsname());
        holder.tvBuyNum.setText(mAdapterData.get(position).getBuy_num());
        holder.tvAcreage.setText(mAdapterData.get(position).getAcreage());
        holder.tvPrice.setText(mAdapterData.get(position).getGoodsprice());
        holder.tvArea.setText(mAdapterData.get(position).getArea());
        holder.tvStock.setText(mAdapterData.get(position).getStock());

        holder.itemView.setTag(position);
        holder.btnCollect.setTag(position);
    }

    @Override
    public int getItemCount() {
        if (mAdapterData == null) {
            return 0;
        }
        return mAdapterData.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvName, tvBuyNum, tvAcreage, tvPrice, tvArea, tvStock;
        Button btnCollect;

        public ViewHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.user_collect_name);
            tvBuyNum = itemView.findViewById(R.id.user_collect_buy_num);
            tvAcreage = itemView.findViewById(R.id.user_collect_acreage);
            tvPrice = itemView.findViewById(R.id.user_collect_price);
            tvArea = itemView.findViewById(R.id.user_collect_area);
            tvStock = itemView.findViewById(R.id.user_collect_stock);
            btnCollect = itemView.findViewById(R.id.user_collect_collect);
            itemView.setOnClickListener(itemOnClickListener);
            btnCollect.setOnClickListener(collectOnClickListener);
        }
    }

    //copy item
    private onItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(onItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    private View.OnClickListener itemOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(v, (Integer) v.getTag());
            }
        }
    };

    private View.OnClickListener collectOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onCollectClick(v, (Integer) v.getTag());
            }
        }
    };

    public interface onItemClickListener {
        void onItemClick(View view, int position);

        void onCollectClick(View view, int position);
    }
}
